package com.peng.leetcode.other;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * MaxSlidingWindow
 *
 * @author: lupeng6
 * @create: 2021/1/2 21:03
 */
public class MaxSlidingWindow {

    public static void main(String[] args) {
//        int[] arr = {1,3,-1,-3,5,3,6,7};
//        int k = 3;

        int[] arr = {1,3,1,2,0,5};
        int k = 3;
        int[] ints = maxSlidingWindow(arr, k);
        Arrays.stream(ints).forEach(System.out::println);
    }

    public static int[] maxSlidingWindow(int[] nums, int k) {
        if (nums == null || nums.length <= 0) {
            return new int[0];
        }
        System.out.println(nums.length + " " + k);
        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < nums.length - k + 1; i++) {
            int index = maxValueIndex(nums, i, i + k);
            result.add(nums[index]);
        }
        int[] arr = new int[result.size()];
        for (int i = 0; i < result.size(); i++) {
            arr[i] = result.get(i);
        }
        return arr;
    }

    public static int maxValueIndex(int[] nums, int start, int end) {
        int index = start;
        int maxLength = Math.min(nums.length, end);
        for (int j = start; j < maxLength; j++) {
            if (nums[index] < nums[j]) {
                index = j;
            }
        }
        return index;
    }
}
